﻿using furion.blog.Application.Contracts;
using furion.blog.Domain.Query.TagConfig;
using furion.blog.Domain.Shared.Attribute;
using furion.blog.Domain.Shared.Dto.Menu;
using furion.blog.Domain.Shared.Dto.TagConfig;
using furion.blog.Domain.Shared.Enum;
using furion.blog.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace furion.blog.Web.Entry.Api
{
    /// <summary>
    /// 系统相关接口
    /// </summary>
    [ApiDescriptionSettings(Groups = new string[] { "TagConfigApi" })]
    [Route("api/tags")]
    public class TagConfigApiController
    {
        public readonly ISystemService _systemService;
        public readonly ITagConfigService _tagConfigService;

        public TagConfigApiController(ISystemService systemService, ITagConfigService tagConfigService)
        {
            this._systemService = systemService;
            this._tagConfigService = tagConfigService;
        }

        [ActionAttribute(Name = "标签分组接口", Desc = "标签分组信息")]
        [HttpGet, Route("groups")]
        public async Task<IActionResult> tagsGroups()
        {
            var ret = await TagsGroup.Items();
            return new OkObjectResult(ResultUtil.Success(ret));
        }

        [ActionAttribute(Name = "删除标签接口", Desc = "删除标签")]
        [HttpDelete, Route("{id}")]
        public async Task<IActionResult> deleteTag(long id,[FromHeader(Name = "UserId")]long userId) {
            await this._tagConfigService.Delete(id,userId);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "修改标签状态接口", Desc = "修改标签状态")]
        [HttpPost, Route("{id}/{status}")]
        public async Task<IActionResult> changeStatus([FromRoute(Name = "id")]long id,[FromRoute(Name = "status")]string status,[FromHeader(Name = "UserId")]long userId) {
            await this._tagConfigService.ChangeStatus(id,status,userId);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "标签详情接口", Desc = "获取标签详细信息")]
        [HttpGet, Route("{id}")]
        public async Task<IActionResult> detail([FromRoute(Name = "id")] long id) { 
            var ret = await this._tagConfigService.Detail(id);
            return new OkObjectResult(ResultUtil.Success(ret));
        }


        [ActionAttribute(Name = "新增标签接口", Desc = "新增标签")]
        [HttpPost, Route("add")]
        public async Task<IActionResult> add([FromBody]TagConfigCreateDto dto,[FromHeader(Name = "UserId")]long userId) {
            dto.CreatedBy = dto.UpdatedBy = userId;
            dto.CreatedTime = dto.UpdatedTime = DateTime.Now;
            await this._tagConfigService.Add(dto);
            return new OkObjectResult(ResultUtil.Success());
        }


        [ActionAttribute(Name = "更新标签接口", Desc = "更新标签")]
        [HttpPost, Route("update")]
        public async Task<IActionResult> update([FromBody] TagConfigUpdateDto dto, [FromHeader(Name = "UserId")] long userId)
        {
            dto.UpdatedBy = userId;
            await this._tagConfigService.Update(dto);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "获取所有标签接口", Desc = "根据group获取对应标签,没有group返回所有标签")]
        [HttpGet, Route("all"), AllowAnonymous]
        public async Task<IActionResult> all([FromQuery(Name = "group")]string group) {
            var ret = await this._tagConfigService.All(group);
            return new OkObjectResult(ResultUtil.Success(ret));
        }


        [ActionAttribute(Name = "标签分页接口", Desc = "标签分页数据")]
        [HttpGet, Route("list")]
        public async Task<IActionResult> tagsList(
            [FromQuery(Name = "page")] int pageIndex,
            [FromQuery(Name = "limit")] int pageSize,
            [FromQuery(Name = "searchContent")] string searchContent = "",
            [FromQuery(Name = "group")] string group = "",
            [FromQuery(Name = "status")] string status = ""
            )
        {
            TagConfigListQuery query = new TagConfigListQuery
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                SearchContent = searchContent,
                Group = group,
                Status = status,
                IsDelete = YesOrNoEnum.No.ToString(),
            };
            var ret = await this._tagConfigService.List(query);
            return new OkObjectResult(ResultUtil.Success(ret));
        }
    }
}
